package audit

import (
	"center/db"
	"context"
	"go.uber.org/zap"
	"time"
)

type Service struct {
	db  *db.CenterDB
	log *zap.Logger
}

func NewService(log *zap.Logger, centerDb *db.CenterDB) *Service {
	return &Service{
		log: log,
		db:  centerDb,
	}
}

func (w *Service) Run(ctx context.Context) error {
	tk := time.NewTimer(time.Second)
	defer tk.Stop()
	for {
		select {
		case <-ctx.Done():
			return ctx.Err()
		case <-tk.C:
			if err := w.worker(ctx); err != nil {
				return err
			}
			tk.Reset(time.Second * 60)
		}
	}
}

func (w *Service) worker(ctx context.Context) error {
	_, err := w.db.DeleteObjectExpire(ctx)
	if err != nil {
		w.log.Error("删除过期object失败", zap.Error(err))
	}
	return nil
}
